package com.wc.算法提高课.C第三章_图论.有向图的强连通分量.受欢迎的牛;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/21 16:17
 * @description https://www.acwing.com/problem/content/1176/
 */
public class Main {
    /**
     * tarjan 算法计算强连同分量<p>
     * 缩点 + 拓扑最后一个值<p>
     * 拓扑之后, 可以判断出出度为 0的点为最后一个缩点, 就是前面牛都喜欢的, 需要所有牛都喜欢, 那就只能有一个出度为0的缩点, 缩点中点的数量就是答案<p>
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 10010, M = 50010;
    static int[] h = new int[N], e = new int[M], ne = new int[M];
    // dfn[u] 表示自己的时间戳, low[u] 它所在的强连通分量中时间戳最小的点的时间戳
    static int[] dfn = new int[N], low = new int[N], stack = new int[N];
    // id[i] 表示自己是属于哪一个连通分量的, size[i] 每个强连通分量中点的个数
    static int[] id = new int[N], size = new int[N], dout = new int[N];
    static boolean[] instack = new boolean[N];
    static int idx = 1, timestamp = 0, scc_cnt = 0, top = 0;
    static int n, m;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        while (m-- > 0) {
            int a = sc.nextInt(), b = sc.nextInt();
            add(a, b);
        }
        for (int i = 1; i <= n; i++) {
            if (dfn[i] == 0) tarjan(i);
        }
        // 计算出度
        for (int u = 1; u <= n; u++) {
            for (int i = h[u]; i > 0; i = ne[i]) {
                int j = e[i];
                int a = id[u], b = id[j];
                if (a != b) dout[a]++;
            }
        }
        int zeros = 0, sum = 0;
        for (int i = 1; i <= scc_cnt; i++) {
            if (dout[i] == 0) {
                zeros++;
                sum += size[i];
            }
            if (zeros > 1) {
                sum = 0;
                break;
            }
        }
        out.println(sum);
        out.flush();
    }

    static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }

    static void tarjan(int u) {
        dfn[u] = low[u] = ++timestamp;
        stack[++top] = u;
        instack[u] = true;
        for (int i = h[u]; i > 0; i = ne[i]) {
            int j = e[i];
            if (dfn[j] == 0) {
                tarjan(j);
                low[u] = Math.min(low[u], low[j]);
            } else if (instack[j]) low[u] = Math.min(low[u], dfn[j]);
        }
        // 说明他自己是连通分量的头
        if (dfn[u] == low[u]) {
            int y;
            ++scc_cnt;
            do {
                y = stack[top--];
                instack[y] = false;
                id[y] = scc_cnt;
                size[scc_cnt]++;
            } while (y != u);
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
